package main

import "github.com/gin-gonic/gin"

type User struct {
	Id string `form:"id" binding:"required,uuid"`
}

// 数据校验
func main() {
	r := gin.Default()
	//测试： http://localhost:8080/user/v1?id=de001853-8d8f-4a06-a5ec-e7ccc7d29481
	r.GET("/user/v1", ShouldBindQueryHandler)
	r.GET("/user/v2", bingQueryHandler)
	r.Run()

}
func ShouldBindQueryHandler(c *gin.Context) {
	var user User
	err := c.ShouldBindQuery(&user)
	if err != nil {
		c.JSON(200, gin.H{
			"Code": 200,
			"Msg":  err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code": 200,
		"Msg":  user.Id,
	})
}
func bingQueryHandler(c *gin.Context) {
	var user User
	err := c.BindQuery(&user)
	if err != nil {
		c.JSON(200, gin.H{
			"Code": 200,
			"Msg":  err.Error(),
		})
		return
	}
	c.JSON(200, gin.H{
		"Code": 200,
		"Msg":  user.Id,
	})
}
